import Ember from 'ember';
import Cookies from 'ember-cli-js-cookie';
import TableExport from '../addon/tableExport';

export default Ember.Controller.extend({
  notify: Ember.inject.service(),
  service_PageConstrut:Ember.inject.service("page-constructure"),
  service_dataLoader:Ember.inject.service("data-loader"),
  service_cordova:Ember.inject.service("cordova"),
  mainController: Ember.inject.controller('business.mainpage'),
  sidebarWidth:160,//边栏默认宽度
  sm_duration: 0.2,
  sm_transition_delay: 150,

  isMobile: Ember.computed("global_curStatus",function () {
    console.log("isMobile in:", this.get("global_curStatus").get("isMobile"));
    return this.get("global_curStatus").get("isMobile");
  }),
  /*tree相关*/
  treedata: Ember.computed("model",function () {
    console.log("this.model", this.get("model"));
    return this.get("model").treedata;
  }),
  /*初始化入口*/
  init: function () {
    var _self = this;
    console.log("body in init:", this.get("body"));
    console.log("public_vars:", public_vars);
    Ember.run.schedule("afterRender",this,function() {
      //通用设置
      this.popInit();
      //菜单渲染
      this.sideMenuInit();
      //初始化顶部滑动块
      this.toggleInit();
      //初始化jquery组件定义
      this.initJqueryPlugin();
    });
  },
  layoutInit: function(){
    jQuery('.sidebar').removeClass("hidden");
    jQuery('header').css("width","100%");
    let width = $(window).width();
    console.log("need set width:" + width);
    jQuery('#header').removeClass("navbar-fixed-top");
    this.extendAction(jQuery("#sidebar-collapse"));
    $("#main-content").on('resize', function (e) {
      e.stopPropagation();
    });
  },
  //折叠
  collapseAction: function(collapsedItem){
    let width = $(window).width();
    if(!this.get("media.isDesktop")){
      jQuery('.sidebar').removeClass("hidden");
      jQuery('#header').removeClass("navbar-fixed-top");
      jQuery('header').width(width);
      jQuery('#main-content').css("width",width);
      collapsedItem.attr("collapsed","yes");
    }else{
      /* For Navbar */
      jQuery('.navbar-brand').addClass("mini-menu");
      /* For sidebar */
      jQuery('#sidebar').addClass("mini-menu");
      jQuery('#main-content').addClass("margin-left-50");
      width = width - 50;
      console.log("main width:" + width);
      jQuery('#main-content').width(width);
      collapsedItem.attr("collapsed","yes");
    }
  },
  //展开
  extendAction: function(collapsedItem){
    let width = $(window).width();
    console.log("window width:" + width);
    if(!this.get("media.isDesktop")){
      jQuery('.sidebar').addClass("hidden");
      jQuery('#header').addClass("navbar-fixed-top");
      jQuery('header').css("width","auto");
      jQuery('#main-content').css("width","auto");
      collapsedItem.attr("collapsed","no");
    }else{
      jQuery('.sidebar').removeClass("hidden");
      /* For Navbar */
      jQuery('.navbar-brand').removeClass("mini-menu");
      /* For sidebar */
      jQuery('#sidebar').removeClass("mini-menu");
      jQuery('#main-content').removeClass("margin-left-50");
      width = width - this.get("sidebarWidth");
      console.log("main width:" + width);
      jQuery('#main-content').width(width);
      collapsedItem.attr("collapsed","no");
    }
  },
  /*
  * 初始化常规项
  */
  popInit: function () {
    //获得权限列表
    this.get("service_dataLoader").syncPrivilegeData();
    //加载页面需要设置min height
    $("#pageLoadingDiv").css("minHeight", $(window).height());
    //布局数据初始化
    this.layoutInit();
  },
  /*
  * 初始化顶部滑动块
  */
  toggleInit: function () {
    var _self = this;
    var handleMobileSidebar = function () {
      var menu = $('.sidebar');
      if (menu.parent('.slimScrollDiv').size() === 1) { // destroy existing instance before updating the height
        menu.slimScroll({
          destroy: true
        });
        menu.removeAttr('style');
        $('#sidebar').removeAttr('style');
      }
      menu.slimScroll({
        size: '7px',
        color: '#a1b2bd',
        opacity: 0.3,
        height: "100%",
        allowPageScroll: false,
        disableFadeOut: false
      });
    };
    //屏幕切换复位
    console.log("window width:" + $(window).width() + " and media isTablet:" + this.get("media.isTablet"));
    jQuery("#sidebar-collapse").click(function(){
      var collapsed = $(this).attr("collapsed");
      if(collapsed&&collapsed==="yes"){
        _self.extendAction($(this));
      }else{
        _self.collapseAction($(this));
      }
    });
  },
  /*菜单渲染*/
  sideMenuInit: function(){
    var _self = this;
    let sideWidth = this.get("sidebarWidth");
    jQuery('.sidebar-menu .has-sub > a').click(function () {
      var last = jQuery('.has-sub.open', $('.sidebar-menu'));
      last.removeClass("open");
      jQuery('.arrow', last).removeClass("open");
      jQuery('.sub', last).slideUp(sideWidth);

      var thisElement = $(this);
      var slideOffeset = -1 * sideWidth;
      var slideSpeed = sideWidth;

      var sub = jQuery(this).next();
      if (sub.is(":visible")) {
        jQuery('.arrow', jQuery(this)).removeClass("open");
        jQuery(this).parent().removeClass("open");
        sub.slideUp(slideSpeed, function () {
          if ($('#sidebar').hasClass('sidebar-fixed') == false) {
            // App.scrollTo(thisElement, slideOffeset);
          }
          // handleSidebarAndContentHeight();
        });
      } else {
        jQuery('.arrow', jQuery(this)).addClass("open");
        jQuery(this).parent().addClass("open");
        sub.slideDown(slideSpeed, function () {
          if ($('#sidebar').hasClass('sidebar-fixed') == false) {
            // App.scrollTo(thisElement, slideOffeset);
          }
          // handleSidebarAndContentHeight();
        });
      }
    });
    // Handle sub-sub menus
    jQuery('.sidebar-menu .has-sub .sub .has-sub-sub > a').click(function () {
      var last = jQuery('.has-sub-sub.open', $('.sidebar-menu'));
      last.removeClass("open");
      jQuery('.arrow', last).removeClass("open");
      jQuery('.sub', last).slideUp(sideWidth);
      var sub = jQuery(this).next();
      if (sub.is(":visible")) {
        jQuery('.arrow', jQuery(this)).removeClass("open");
        jQuery(this).parent().removeClass("open");
        sub.slideUp(sideWidth);
      } else {
        jQuery('.arrow', jQuery(this)).addClass("open");
        jQuery(this).parent().addClass("open");
        sub.slideDown(sideWidth);
      }
    });
  },

  /*显示提示信息*/
  popTorMsg: function(msg){
    let _self = this;
    this.set("footerTorMsg",true);
    this.set("toriseMessage",msg);
    $("#footerTor").show();
    $("#footerTor").fadeIn(500,function(){
      Ember.run.later(function(){
        $("#footerTor").fadeOut(1000);
      },3000);
    });
  },
  /*jquery组件定义*/
  initJqueryPlugin:function(){

  },


  actions: {
    initPage: function () {
      this.get('jstreeActionReceiver').send('redraw');
    },
    //主页面跳转
    changeMainPage:function (menuLink) {
      console.log("changeMainPage in,menuLink:" + menuLink);
      this.get("mainController").switchMainPage(menuLink);
    },
  },

  /*移动端导航功能*/
  breadCrumbsPath: null,
  pageTitle: null,
  //根据当前路径，计算导航信息
  curPath: null,//当前路径

});
